/*
 * @Descripttion: 
 * @Author: voanit
 * @Date: 2022-06-07 18:06:00
 * @LastEditors: voanit
 * @LastEditTime: 2022-06-12 09:41:51
 */
import { ArticleDetailAction } from '@/types/store'
import { ArticleDetail, Comment } from '@/types/data'
type State = {
  info: ArticleDetail
  comment: Comment
  reply: Comment
}
const initialState = {
  info: {},
  comment: {},
  reply: {}
} as State
const articleDetail = (state = initialState, action: ArticleDetailAction): State => {
  // console.log(action.payload, 16)
  if (action.type === 'articleDetail/setArticleInfo') {
    return {
      ...state,
      info: action.payload
    }

  } else if (action.type === 'article/saveComment') {
    return {
      ...state,
      comment: {
        ...action.payload,// ...action.payload
        results: [
          // ...state.comment.results || [],
          ...action.payload.results
        ]
      }
    }
  } else if (action.type === 'article/saveNewComment') {
    return {
      ...state,
      comment: {
        ...state.comment,
        results: [action.payload, ...state.comment.results]
      }
    }
  } else if (action.type === 'article/saveReply') {
    // 获取所有的评论列表
    return {
      ...state,
      reply: {
        ...action.payload,// ...action.payload
        results: [
          ...action.payload.results
        ]
      }
    }
  }
  else if (action.type === 'article/saveNewReply') {
    // 获取所有的评论列表
    return {
      ...state,
      reply: {
        ...state.reply,// ...action.payload
        results: [
          action.payload, ...state.reply.results
        ]
      }
    }
  }
  return state

}

export default articleDetail